//
// Created by dddgzc on 2025/1/23.
//

#include "../include/PlayerSelectionScene.h"


void PlayerSelectionScene::onEnter() {
    printf("Entering Player Selection Scene\n");
    // idle动画
    peashooterIdleAnimation.setAtlas(peashooterIdleRightAtlas);
    peashooterIdleAnimation.setInterval(80);
    sunflowerIdleAnimation.setAtlas(sunflowerIdleRightAtlas);
    sunflowerIdleAnimation.setInterval(80);

    leftScrollImageMiddleLineX = Content::WINDOW_WIDTH / 4;
    rightScrollImageMiddleLineX = Content::WINDOW_WIDTH * 3 / 4;
    SDL_QueryTexture(sunflowerSelectorBackground, nullptr, nullptr, &scrollImageWidth, &scrollImageHeight);
    leftPlayerType = SelectPlayerType::SUNFLOWER;
    rightPlayerType = SelectPlayerType::PEASHOOTER;

    leftScrollBackground = peashooterSelectorBackground;
    rightScrollBackground = sunflowerSelectorBackground;
    leftPlayerAnimation = &sunflowerIdleAnimation;
    rightPlayerAnimation = &peashooterIdleAnimation;

    // 初始化屏幕上图片坐标
    leftPlayerAnimationPosition = {200, 250};
    rightPlayerAnimationPosition = {980, 250};
    enterKeyTextPosition = {466, Content::WINDOW_HEIGHT - 100};
}

void PlayerSelectionScene::onDraw(const Camera &camera) {
    // 绘制背景
    drawImage(camera, selectorBackground, 0, 0);
    // 绘制滚动背景
    switch (leftPlayerType) {
        case SelectPlayerType::SUNFLOWER:
            rightScrollBackground = peashooterSelectorBackground;
            break;
        case SelectPlayerType::PEASHOOTER:
            rightScrollBackground = sunflowerSelectorBackground;
            break;
        default:
            break;
    }

    switch (rightPlayerType) {
        case SelectPlayerType::SUNFLOWER:
            leftScrollBackground = peashooterSelectorBackground;
            break;
        case SelectPlayerType::PEASHOOTER:
            leftScrollBackground = sunflowerSelectorBackground;
            break;
        default:
            break;
    }

    // 左侧滚动背景
    // 左侧的左侧原图尺寸
    SDL_Rect leftScrollBackgroundLeftSrcRect = {
        scrollImageWidth - leftScrollImageMiddleLineX, 0, leftScrollImageMiddleLineX, scrollImageHeight
    };
    // 左侧的左侧目标窗口绘制尺寸
    SDL_Rect leftScorllBackgroundLeftDstRect = {0, 0, leftScrollImageMiddleLineX, Content::WINDOW_HEIGHT};
    // 左侧的右侧原图尺寸
    SDL_Rect leftScrollBackgroundRightSrcRect = {
        0, 0, (Content::WINDOW_WIDTH / 2 - 80) - leftScrollImageMiddleLineX, scrollImageHeight
    };
    // 左侧的右侧目标窗口绘制尺寸
    SDL_Rect leftScrollBackgroundRightDstRect = {
        leftScrollImageMiddleLineX, 0, (Content::WINDOW_WIDTH / 2 - 80) - leftScrollImageMiddleLineX,
        Content::WINDOW_HEIGHT
    };
    drawSplit(camera, leftScrollBackground, leftScrollBackgroundLeftSrcRect, leftScorllBackgroundLeftDstRect);
    drawSplit(camera, leftScrollBackground, leftScrollBackgroundRightSrcRect, leftScrollBackgroundRightDstRect);

    // 右侧滚动背景
    // 右侧的左侧原图尺寸
    SDL_Rect rightScrollBackgroundLeftSrcRect = {
        scrollImageWidth - (rightScrollImageMiddleLineX - (Content::WINDOW_WIDTH / 2 + 80)), 0,
        rightScrollImageMiddleLineX - (Content::WINDOW_WIDTH / 2 + 80), scrollImageHeight
    };
    // 右侧的左侧目标窗口绘制尺寸
    SDL_Rect rightScrollBackgroundLeftDstRect = {
        (Content::WINDOW_WIDTH / 2 + 80), 0, rightScrollImageMiddleLineX - (Content::WINDOW_WIDTH / 2 + 80),
        Content::WINDOW_HEIGHT
    };
    // 右侧的右侧原图尺寸
    SDL_Rect rightScrollBackgroundRightSrcRect = {
        0, 0, Content::WINDOW_WIDTH - rightScrollImageMiddleLineX, scrollImageHeight
    };
    // 右侧的右侧目标窗口绘制尺寸
    SDL_Rect rightScrollBackgroundRightDstRect = {
        rightScrollImageMiddleLineX, 0, Content::WINDOW_WIDTH - rightScrollImageMiddleLineX, Content::WINDOW_HEIGHT
    };
    drawSplit(camera, rightScrollBackground, rightScrollBackgroundLeftSrcRect, rightScrollBackgroundLeftDstRect);
    drawSplit(camera, rightScrollBackground, rightScrollBackgroundRightSrcRect, rightScrollBackgroundRightDstRect);

    // 绘制VS
    int w, h;
    SDL_QueryTexture(vs, nullptr, nullptr, &w, &h);
    draw(camera, vs, (Content::WINDOW_WIDTH - w) / 2, (Content::WINDOW_HEIGHT - h) / 2);
    // 绘制墓碑
    SDL_QueryTexture(gravestone, nullptr, nullptr, &w, &h);
    draw(camera, gravestone, 0 + 100, (Content::WINDOW_HEIGHT - h) / 2);
    draw(camera, gravestone, (Content::WINDOW_WIDTH) - w - 100, (Content::WINDOW_HEIGHT - h) / 2);
    // 绘制控制menu
    draw(camera, player1pDesc, 0 + 100, (Content::WINDOW_HEIGHT - h) / 2 + h + 50);
    draw(camera, player2pDesc, (Content::WINDOW_WIDTH) - w - 100, (Content::WINDOW_HEIGHT - h) / 2 + h + 50);
    // 绘制角色动画
    leftPlayerAnimation->onDraw(camera, leftPlayerAnimationPosition.x, leftPlayerAnimationPosition.y);
    rightPlayerAnimation->onDraw(camera, rightPlayerAnimationPosition.x, rightPlayerAnimationPosition.y);
    // 绘制校色title
    draw(camera, player1p, 200, 50);
    draw(camera, player2p, 960, 50);
    // 绘制Tips 回车键开始
    draw(camera, selectorTip, enterKeyTextPosition.x, enterKeyTextPosition.y);

    if (rightPlayerType == SelectPlayerType::SUNFLOWER) {
        rightPlayerAnimation = &sunflowerIdleAnimation;
    } else if (rightPlayerType == SelectPlayerType::PEASHOOTER) {
        rightPlayerAnimation = &peashooterIdleAnimation;
    }

    if (leftPlayerType == SelectPlayerType::SUNFLOWER) {
        leftPlayerAnimation = &sunflowerIdleAnimation;
    } else if (leftPlayerType == SelectPlayerType::PEASHOOTER) {
        leftPlayerAnimation = &peashooterIdleAnimation;
    }
}

void PlayerSelectionScene::onUpdate(int delta) {
    leftScrollImageMiddleLineX += 2;
    rightScrollImageMiddleLineX -= 2;
    if (leftScrollImageMiddleLineX >= (Content::WINDOW_WIDTH / 2 - 80)) {
        leftScrollImageMiddleLineX = 0;
    }
    if (rightScrollImageMiddleLineX <= (Content::WINDOW_WIDTH / 2 + 80)) {
        rightScrollImageMiddleLineX = Content::WINDOW_WIDTH;
    }
    leftPlayerAnimation->onUpdate(delta);
    rightPlayerAnimation->onUpdate(delta);
}

void PlayerSelectionScene::onInput(const SDL_Event &e) {
    if (e.type == SDL_KEYDOWN) {
        switch (e.key.keysym.sym) {
            case SDLK_LEFT:
            case SDLK_RIGHT:
                // 右边角色切换
                rightPlayerType = rightPlayerType == SelectPlayerType::PEASHOOTER
                                      ? SelectPlayerType::SUNFLOWER
                                      : SelectPlayerType::PEASHOOTER;
                break;
            case SDLK_d:
            case SDLK_a:
                // 左边角色切换
                leftPlayerType = leftPlayerType == SelectPlayerType::SUNFLOWER
                                     ? SelectPlayerType::PEASHOOTER
                                     : SelectPlayerType::SUNFLOWER;
                break;
            case SDLK_RETURN:
                sceneManager->switchTo(SceneManager::SceneType::GameScene);
                break;
            default:
                break;
        }
    }
}

void PlayerSelectionScene::onExit() {
    switch (leftPlayerType) {
        case SelectPlayerType::SUNFLOWER:
            player01 = new SunflowerPlayer();
            player01Avatar = avatarSunflower;
            break;
        case SelectPlayerType::PEASHOOTER:
            player01 = new PeashooterPlayer();
            player01Avatar = avatarPeashooter;
            break;
        default:
            player01 = new SunflowerPlayer();
            player01Avatar = avatarSunflower;
            break;
    }
    player01->setPlayerID(PlayerID::PLAYER_1);

    switch (rightPlayerType) {
        case SelectPlayerType::SUNFLOWER:
            player02 = new SunflowerPlayer();
            player02Avatar = avatarSunflower;
            break;
        case SelectPlayerType::PEASHOOTER:
            player02 = new PeashooterPlayer();
            player02Avatar = avatarPeashooter;
            break;
        default:
            player02 = new PeashooterPlayer();
            player02Avatar = avatarPeashooter;
            break;
    }
    player02->setPlayerID(PlayerID::PLAYER_2);
}
